package com.atguigu.educms.controller;


import com.atguigu.commonutils.R;
import com.atguigu.educms.entity.CrmBanner;
import com.atguigu.educms.query.BannerQuery;
import com.atguigu.educms.service.CrmBannerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

/**
 * <p>
 * 首页banner表 前端控制器
 * </p>
 *
 * @author gengky
 * @since 2021-09-22
 */
@RestController
@RequestMapping("/educms/banneradmin")
//@CrossOrigin
public class BannerAdminController {

    @Autowired
    private CrmBannerService bannerService;

    @Autowired
    RedisTemplate redisTemplate;

    @ApiOperation(value = "分页轮播图列表")
    @PostMapping("PageCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) BannerQuery bannerQuery){

        //创建page对象
        Page<CrmBanner> pageTeacher=new Page<>(current,limit);
        //构建条件
        QueryWrapper<CrmBanner> wrapper=new QueryWrapper<>();

        System.out.println("----------");
        System.out.println(current+"---"+limit);

        //多条件组合查询（动态sql组合）
        if (!StringUtils.isEmpty(bannerQuery.getTitle())){
            //构造条件（这里“name”是mysql中的列名，后面一样）
            wrapper.like("title",bannerQuery.getTitle());
        }
        if (!StringUtils.isEmpty(bannerQuery.getIsDeleted())){
            wrapper.eq("is_deleted",bannerQuery.getIsDeleted());
        }
        if (!StringUtils.isEmpty(bannerQuery.getBegin())){
            wrapper.ge("gmt_create",bannerQuery.getBegin());
        }
        if (!StringUtils.isEmpty(bannerQuery.getEnd())){
            wrapper.le("gmt_create", bannerQuery.getEnd());
        }
        //排序
        wrapper.orderByDesc("gmt_create");
        //调用方法实现条件查询分页
        bannerService.page(pageTeacher,wrapper);
        //
        List<CrmBanner> records = pageTeacher.getRecords();
        //
        long total=pageTeacher.getTotal();

        return R.ok().data("total",total).data("rows",records);

    }


    //根据ID禁用轮播图
    @PostMapping("unUseBannerById/{bannerId}")
    public R unUserBannerById(@PathVariable String bannerId){
        CrmBanner crmBanner = bannerService.getById(bannerId);
        crmBanner.setIsDeleted(true);
        bannerService.saveOrUpdate(crmBanner);
        redisTemplate.delete("banner::selectIndexList");
        return R.ok();
    }
    //根据ID启用轮播图
    @PostMapping("reUseBannerById/{bannerId}")
    public R reUserBannerById(@PathVariable String bannerId){
        CrmBanner crmBanner = bannerService.getById(bannerId);
        crmBanner.setIsDeleted(false);
        bannerService.saveOrUpdate(crmBanner);
        redisTemplate.delete("banner::selectIndexList");
        return R.ok();
    }




    /**
     * 分页查询banner
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("pageBanner/{page}/{limit}")
    public R pageBanner(@PathVariable long page,@PathVariable long limit) {
        Page<CrmBanner> pageBanner = new Page<>(page,limit);
        bannerService.page(pageBanner,null);
        return R.ok().data("items",pageBanner.getRecords()).data("total",pageBanner.getTotal());
    }

    /**
     * 添加banner
     * @param crmBanner
     * @return
     */
    @PostMapping("addBanner")
    public R addBanner(@RequestBody CrmBanner crmBanner) {
        crmBanner.setGmtCreate(new Date());
        crmBanner.setGmtModified(new Date());
        bannerService.save(crmBanner);
        redisTemplate.delete("banner::selectIndexList");
        return R.ok();
    }

    @ApiOperation(value = "获取Banner")
    @GetMapping("get/{id}")
    public R get(@PathVariable String id) {
        CrmBanner banner = bannerService.getById(id);
        return R.ok().data("item", banner);
    }

    @ApiOperation(value = "修改Banner")
    @PutMapping("update")
    public R updateById(@RequestBody CrmBanner banner) {
        bannerService.updateById(banner);
        redisTemplate.delete("banner::selectIndexList");
        return R.ok();
    }

    @ApiOperation(value = "删除Banner")
    @DeleteMapping("remove/{id}")
    public R remove(@PathVariable String id) {
        bannerService.removeById(id);
        redisTemplate.delete("banner::selectIndexList");
        return R.ok();
    }

}

